<?php
/* @var $this yii\web\View */
use yii\helpers\Url;
use yii\helpers\Html;
use app_supplier\assets\SuppAssets;
use app_supplier\helpers\PurchaseHelper;
use yii\widgets\LinkPager;
use yii\widgets\LinkSorter;
SuppAssets::addArrayCss($this, [
    '@web/css/purchase-index.css',
    '@web/css/pur-index.css',
    '@web/css/page.css',
]);
SuppAssets::addArrayScript($this,  [
    '@web/js/jsAddress.js',
], $this::POS_HEAD);
$dealer_info = Yii::$app->session->get('user_dealer');
?>
<div class="box">
    <div class="heading">
        <h1><img src="<?=Url::to('@web/images/')?>category.png" alt=""><?=$this->context->pageTitle?></h1>
        <div class="buttons">
            <a onclick="javascript:purchase.showDiv(); " class="button" id="condition-name"><span>隐藏筛选条件</span></a>
            <a class="button " href="<?=Url::to(['main/index'])?>"><span>返回</span></a>
        </div>
    </div>
    <div class="content">
        <div class="dashboard-content" style="margin-bottom: 15px;" id="condition">
            <form action="<?=Url::to([$this->context->id .'/'.$this->context->action->id])?>" method="get">

                <!--div class="row-1">
                    <div class="row-12">
                        <span class="tip">全&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;部：</span>
                    </div>
                    <div class="row-2">
                        <?php //if (isset($condition['time']) && $condition['time']=='no-condition'):?>
                            <input type="radio" name="time" value="no-condition" checked>
                        <?php //else:?>
                            <input type="radio" name="time" value="no-condition">
                        <?php //endif;?>

                    </div>
                    <div class="clear-fox"></div>
                </div>
                <hr class="hr-dashed"-->
                <div class="row-1">
                    <div class="row-12">
                        <span class="tip">选择区域：</span>
                    </div>
                    <div class="row-2" id="distpicker">
                        <select name="cmbProvince" id="cmbProvince" style="width: 25%">

                        </select>
                        <select name="cmbCity" id="cmbCity"  style="width: 25%">

                        </select>
                        <select name="cmbArea" id="cmbArea"  style="width: 25%">
                        </select>
                    </div>
                    <div class="clear-fox"></div>
                </div>
                <hr class="hr-dashed">
                <div class="row-1">
                    <div class="row-12">
                        <span class="tip">选择时间：</span>
                    </div>
                    <div class="row-2">
                        <?php if (isset($condition['time']) && $condition['time']=='all'):?>
                            <input type="radio" name="time"  value="all" checked>&nbsp;&nbsp;所有时间
                        <?php else:?>
                            <input type="radio" name="time"  value="all">&nbsp;&nbsp;所有时间
                        <?php endif;?>

                        <?php if (isset($condition['time']) && $condition['time']=='today'):?>
                            <input type="radio" name="time" class="time" value="today" checked>&nbsp;&nbsp;今日
                        <?php else:?>
                            <input type="radio" name="time" class="time" value="today">&nbsp;&nbsp;今日
                        <?php endif;?>

                        <?php if (isset($condition['time']) && $condition['time']=='mouth'):?>
                            <input type="radio" name="time" class="time" value="mouth" checked>&nbsp;&nbsp;本月
                        <?php else:?>
                            <input type="radio" name="time" class="time" value="mouth">&nbsp;&nbsp;本月
                        <?php endif;?>

                        <?php if (isset($condition['time']) && $condition['time']=='period'):?>
                            <input type="radio" name="time" class="time" value="period" checked>&nbsp;&nbsp;期间
                        <?php else:?>
                            <input type="radio" name="time" class="time" value="period">&nbsp;&nbsp;期间
                        <?php endif;?>
                        <input type="text" name="start" class="date" id="stat_start" value="<?=$condition['start']?>">&nbsp;&nbsp;至
                        <input type="text" name="end" class="date" id="stat_end" value="<?=$condition['end']?>">
                    </div>
                    <div class="clear-fox"></div>
                </div>
                <hr class="hr-dashed">
                <div class="row-1">
                    <div class="row-12">
                        <span class="tip">订单状态：</span>
                    </div>
                    <div class="row-2">
                        <select name="status" id="" class="select-number">
                            <?php if (isset($order_info['order_state']) && is_array($order_info['order_state'])):?>
                                <?php foreach ($order_info['order_state'] as $state):?>
                                    <?php if (isset($condition['status']) && $condition['status']==$state['id']):?>
                                        <option value="<?=$state['id']?>" selected><?=$state['name']?></option>
                                    <?php else:?>
                                        <option value="<?=$state['id']?>"><?=$state['name']?></option>
                                    <?php endif;?>
                                <?php endforeach;?>
                            <?php else:?>
                                <option value="1">所有订单</option>
                            <?php endif;?>
                        </select>
                    </div>
                    <div class="clear-fox"></div>
                </div>
                <hr class="hr-dashed">
                <div class="row-1">
                    <div class="row-12">
                        <span class="tip">报价状态：</span>
                    </div>
                    <div class="row-2">
                        <select name="price_status" id="" class="select-number">
                            <?php if (isset($order_info['price_list']) && is_array($order_info['price_list'])):?>
                                <?php foreach ($order_info['price_list'] as $state):?>
                                    <?php if (isset($price_status) && $price_status==$state['id']):?>
                                        <option value="<?=$state['id']?>" selected><?=$state['name']?></option>
                                    <?php else:?>
                                        <option value="<?=$state['id']?>"><?=$state['name']?></option>
                                    <?php endif;?>
                                <?php endforeach;?>
                            <?php else:?>
                                <option value="0">所有订单</option>
                            <?php endif;?>
                        </select>
                    </div>
                    <div class="clear-fox"></div>
                </div>
                <hr class="hr-dashed">
                <div class="row-1">
                    <div class="row-12">
                        <span class="tip"></span>
                    </div>
                    <div class="row-2" style="">
                        <a onclick="javascript:purchase.saveSubmit(); " class="button my-button" style="margin-left: 15%;"><span>查询</span></a>
                        <a onclick="javascript:purchase.cancelCondition()" class="button my-button" style="margin-left: 10%;"><span>取消</span></a>
                    </div>
                    <div class="clear-fox"></div>
                </div>
                <input type="hidden" name="t" value="condition">
            </form>
        </div>
        <table id="tbl_list" class="list">
            <thead>
            <tr>
                <th class="center" width="1">
                    <input id="unchecked" onclick="$('input[name*=\'selected\']:not(:disabled)').attr('checked', this.checked);" type="checkbox">
                </th>
                <th class="center" width="55">
                    区&nbsp;&nbsp;域
                </th>
                <th class="center">适配车型</th>
                <th class="center">配件名称</th>
                <th class="center">配件类别</th>
                <th class="center">配件品牌</th>
                <th class="center">配件型号</th>
                <th class="center" width="70">
                    <?php if (isset($sort) && $sort=='material_number'):?>
                        <a href="<?=$sortUrl['material_number']?>" class="<?=$order?>">采购数量</a>
                    <?php else:?>
                        <a href="<?=$sortUrl['material_number']?>">采购数量</a>
                    <?php endif;?>
                </th>
                <th class="center" width="80">
                    <?php if (isset($sort) && $sort=='material_price'):?>
                        <a href="<?=$sortUrl['material_price']?>" class="<?=$order?>">采购单价</a>
                    <?php else:?>
                        <a href="<?=$sortUrl['material_price']?>">采购单价</a>
                    <?php endif;?>
                </th>
                <th class="center" width="80">
                    <?php if (isset($sort) && $sort=='demand_time'):?>
                        <a href="<?=$sortUrl['demand_time']?>" class="<?=$order?>">需求日期</a>
                    <?php else:?>
                        <a href="<?=$sortUrl['demand_time']?>">需求日期</a>
                    <?php endif;?>

                </th>
                <th class="center" width="70">
                    <?php if (isset($sort) && $sort=='days'):?>
                        <a href="<?=$sortUrl['days']?>" class="<?=$order?>">剩余天数</a>
                    <?php else:?>
                        <a href="<?=$sortUrl['days']?>">剩余天数</a>
                    <?php endif;?>
                </th>
                <th class="center" width="60">订单状态</th>
                <!--th class="center" width="55">联系人</th-->
                <th class="center" width="60">操作</th>
            </tr>
            </thead>
            <tbody>

            <?php if(isset($list) && is_array($list)):?>
                <?php foreach ($list as $key=>$row):?>
                    <tr>
                        <td class="center">
                            <input name="selected[]" value="<?=$row['id']?>" type="checkbox">
                        </td>
                        <td class="center"><?=$row['address_city']?></td>
                        <td class="center"><?=$row['car_type']?></td>
                        <td class="center"><?=$row['material_name']?></td>
                        <td class="center"><?=$row['dealer_material_type']?></td>
                        <td class="center"><?=$row['material_brand']?></td>
                        <td class="center"><?=$row['material_version']?></td>
                        <td class="center"><?=$row['material_number']?></td>
                        <td class="center"><?=$row['material_price']?></td>
                        <td class="center"><?=$row['demand_time']?></td>
                        <td class="center"><?=$row['days']?></td>
                        <td class="center">
                            <?php //if ($row['days'] < 0):?>
                            <?php //else:?>
                                <?=$row['status_info']?>
                            <?php //endif; ?>
                        </td>
                        <!--td class="center"><?php //$row['user_name']?></td-->
                        <td class="center" style="text-align: center" id="open_<?=$row['m_id']?>">

                            <?php if (isset($row['state'])):?>
                                <?php if ($row['state']==0):?>
                                    <a href="javascript:purchase.open('<?=$row['m_id']?>')" style="color: #4D90FE;" >我已报价</a>
                                <?php else:?>
                                    <a href="javascript:purchase.open('<?=$row['m_id']?>')" style="color: #4D90FE;" >点击查看</a>
                                <?php endif;?>
                            <?php else:?>
                                <?php $price_status = PurchaseHelper::getDealerUser($row['m_id'], $row['id'], $dealer_info['dealer_id'], Yii::$app->user->id);?>
                                <?php if (!empty($price_status)):?>
                                    <a href="javascript:purchase.open('<?=$row['m_id']?>')" style="color: #4D90FE;" >我已报价</a>
                                <?php else:?>
                                    <a href="javascript:purchase.open('<?=$row['m_id']?>')" style="color: #4D90FE;" >点击查看</a>
                                <?php endif;?>
                            <?php endif;?>

                            <div id="mobile-<?=$row['m_id']?>" class="mo-info">
                                <div class="row-1 row-title">
                                    <div class="row-12 row-list" >
                                        <span class="tip">客户姓名：</span>
                                    </div>
                                    <div class="row-2">
                                        <span><?=$row['user_name']?></span>
                                    </div>
                                    <div class="clear-fox"></div>
                                </div>
                                <hr class="hr-dashed my-hr">
                                <div class="row-1 row-auto">
                                    <div class="row-12 row-list">
                                        <span class="tip">联系电话：</span>
                                    </div>
                                    <div class="row-2">
                                        <span><?=$row['mobile']?></span>
                                    </div>
                                    <div class="clear-fox"></div>
                                </div>
                                <hr class="hr-dashed my-hr">
                                <div class="row-1 row-auto">


                                    <?php if (isset($row['state'])):?>
                                        <?php if ($row['state']==0):?>
                                            <div class="row-6" style="text-align: left !important;">
                                                <a onclick="javascript:purchase.cancelPrice('<?=$row['m_id']?>', '<?=$row['id']?>')" class="button"><span>取消报价</span></a>
                                            </div>
                                        <?php else:?>
                                            <div class="row-6" style="text-align: left !important;">
                                                <a onclick="javascript:purchase.openPrice('<?=$row['m_id']?>', '<?=$row['id']?>')"  class="button"><span>我要报价</span></a>
                                            </div>
                                        <?php endif;?>
                                    <?php else:?>
                                        <?php if (!empty($price_status)):?>
                                            <div class="row-6" style="text-align: left !important;">
                                                <a onclick="javascript:purchase.cancelPrice('<?=$row['m_id']?>', '<?=$row['id']?>')" class="button"><span>取消报价</span></a>
                                            </div>
                                        <?php else:?>
                                            <div class="row-6" style="text-align: left !important;">
                                                <a onclick="javascript:purchase.openPrice('<?=$row['m_id']?>', '<?=$row['id']?>')"  class="button"><span>我要报价</span></a>
                                            </div>
                                        <?php endif;?>

                                    <?php endif;?>


                                    <div class="clear-fox"></div>
                                </div>
                            </div>
                        </td>
                    </tr>
                <?php endforeach;?>
            <?php else:?>
                <tr>
                    <td class="center">
                        <input name="selected[]" value="2" type="checkbox">
                    </td>
                    <td class="center" colspan="13">
                        没有相关的记录
                    </td>

                </tr>
            <?php endif;?>
            </tbody>
        </table>
        <?php if (isset($page) && !empty($page)):?>
            <?=LinkPager::widget([
                'pagination' => $page,
                'nextPageLabel' => '下一页',
                'prevPageLabel' => '上一页',
                'firstPageLabel' => '首页',
                'lastPageLabel' => '尾页',
            ])?>
        <?php endif;?>

    </div>
</div>
<script>
    $(function () {
        addressInit('cmbProvince', 'cmbCity', 'cmbArea',"<?=$condition['cmbProvince']?>" , '<?=$condition['cmbCity']?>', '<?=$condition['cmbArea']?>');
        $('.date').datepicker({
            onSelect: function(dateText) {
                stat('distributor_during');
            }
        });

        function stat(stat_type)
        {
            if (stat_type == 'distributor_during') {
                var date_start = $("#stat_start").val();
                var date_end = $("#stat_end").val();
                if (!date_start || !date_start) {
                    return ;
                }
                start = new Date(date_start);
                end = new Date(date_end);

                if (Date.parse(end) < Date.parse(start)) {
                    layer.msg('结束时间不能早于开始时间！');
                    return;
                } else {
                    layer.msg('时间');
                }

//                url += '&stat_start=' + date_start;
//                url += '&stat_end=' + date_end;
//                console.log(url);
            }
            //location = url;
        }
    });
    var index = '';
    var purchase = {
        showDiv:function () {
            var obj = $("#condition");
            if (obj.is(":hidden")) {
                obj.show();
                $("#condition-name").html('隐藏筛选条件');
            } else {
                $("#condition-name").html('显示筛选条件');
                obj.hide();
            }
        },
        open:function (obj) {
            index = layer.open({
                type: 1,
                title: ['联系报价', 'font-size:12px;text-align:left;'],
                content: $('#mobile-'+obj),
                area:['400px', '235px']
            })
        },
        saveSubmit:function () {
            
            var obj = $("input[type='radio']:checked").val();
            if (obj == 'period') {
                var date_start = $("#stat_start").val();
                var date_end = $("#stat_end").val();
                if (!date_start || !date_start) {
                    return ;
                }
                start = new Date(date_start);
                end = new Date(date_end);

                if (Date.parse(end) < Date.parse(start)) {
                    layer.msg('结束时间不能早于开始时间！');
                    return;
                }
            }

            $('form').submit();
        },
        openPrice:function (mid, dpid) {
            $.get("<?=Url::to([$this->context->id. '/dispose', 'action'=>'offer'])?>",{id : mid, did:dpid}, function (response) {
                layer.msg(response.msg, {icon:1, time:2000},  function () {
                   layer.closeAll();
                    if (response.status == 200) {
                        location.reload();
                    }
                });
            })
        },
        cancelPrice:function (mid, dpid) {
            $.get("<?=Url::to([$this->context->id. '/dispose', 'action'=>'cancel'])?>",{id : mid, did:dpid}, function (response) {
                layer.msg(response.msg, {icon:1, time:2000},  function () {
                    layer.closeAll();
                    if (response.status == 200) {
                        location.reload();
                    }
                });
            })
        },
        cancelCondition:function () {
            var url = $('form').attr('action');
            var ref = 'time';
            location.href = this.delParams(url, ref)+"?t=all";
        },
        delParams:function (url, ref) {
            var str = "";
            if (url.indexOf('?') != -1) {
                str = url.substr(url.indexOf('?') + 1);
            }
            else {
                return url;
            }
            var arr = "";
            var return_url = "";
            if (str.indexOf('&') != -1) {
                arr = str.split('&');
                for (i in arr) {
                    if (arr[i].split('=')[0] != ref) {
                        return_url = return_url + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
                    }
                }
                return url.substr(0, url.indexOf('?')) + "?" + return_url.substr(0, return_url.length - 1);
            } else {
                arr = str.split('=');
                if (arr[0] == ref) {
                    return url.substr(0, url.indexOf('?'));
                }
                else {
                    return url+"?t=all";
                }
            }
        }
    };


</script>
